<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <title>boost 搜索引擎</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap');

        body {
            padding: 20px;
            background: #f5f5f5;
            font-family: 'Roboto', sans-serif;
        }

        .title {
            font-size: 48px;
            color: #4e6ef2;
            text-align: center;
            margin-bottom: 40px;
            letter-spacing: 2px;
            padding-right: 170px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        .search-input {
            border-radius: 50px;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            width: 80%;
            box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2);
        }

        .search-button {
            border-radius: 50px;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            background: linear-gradient(to right, #4e6ef2, #3951b2);
            color: #FFF;
            cursor: pointer;
            margin-left: 10px;
            box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
        }

        .search-button:hover {
            transform: translateY(-2px);
        }

        .search-button:active {
            transform: translateY(0);
        }

        .result .item {
            border-bottom: 1px solid #CCC;
            padding: 20px 0;
            transition: all 0.3s ease;
        }

        .result .item:hover {
            background: #f0f0f0;
        }

        .result .item a {
            color: #4e6ef2;
            font-size: 20px;
            text-decoration: none;
        }

        .result .item a:hover {
            text-decoration: underline;
        }

        .result .item p {
            font-size: 16px;
            color: #333;
        }

        .result .item i {
            font-size: 14px;
            color: green;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1 class="title">boost 搜索引擎</h1>
        <div class="search">
            <input class="search-input" type="text" placeholder="请输入搜索关键字...">
            <button class="search-button" onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
        </div>
    </div>
    <script>
        function Search() {
            // 是浏览器的一个弹出框
            // alert("hello js!");
            // 1. 提取数据, $可以理解成就是JQuery的别称
            let query = $(".container .search input").val();
            // console.log("query = " + query); //console是浏览器的对话框，可
            //2. 发起http请求,ajax: 属于一个和后端进行数据交互的函数，JQuery
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function (data) {
                    console.log(data);
                    BuildHtml(data);
                }
            });
        }
        function BuildHtml(data) {
            // 获取html中的result标签
            let result_lable = $(".container .result");
            // 清空历史搜索结果
            result_lable.empty();
            for (let elem of data) {
                // console.log(elem.title);
                // console.log(elem.url);
                let a_lable = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    // 跳转到新的页面
                    target: "_blank"
                });
                let p_lable = $("<p>", {
                    text: elem.content
                });
                let i_lable = $("<i>", {
                    text: elem.url
                });
                let div_lable = $("<div>", {
                    class: "item"
                });
                a_lable.appendTo(div_lable);
                p_lable.appendTo(div_lable);
                i_lable.appendTo(div_lable);
                div_lable.appendTo(result_lable);
            }
        }
    </script>
</body>

</html>